import amap from '@/access/amap-wx.js';
import QQMapWX from '@/access/qqmap-wx-jssdk.js';
import permision from "@/access/wa-permission/permission.js"
import webUrl from '../config.js';
export default {
	onPageScroll(obj) {
		// console.log(obj)
		this.navFixed = (obj.scrollTop > 1)
		// console.log(this.navFixed)
	},
	computed: {
		barHeight() {
			const systemInfo = uni.getSystemInfoSync();
			return systemInfo.statusBarHeight
		},
		theme() {
			if (this.$store.state.baseSet && this.$store.state.baseSet.themeStyle) {
				return this.$store.state.baseSet.themeStyle.theme || 'red'
			} else {
				return 'red'
			}
		},
		primaryColor() {
			if (this.$store.state.baseSet && this.$store.state.baseSet.themeStyle) {
				return this.$store.state.baseSet.themeStyle.color_o
			} else {
				return '#FD463E'
			}
		},
		primaryJb() {
			if (this.$store.state.baseSet && this.$store.state.baseSet.themeStyle) {
				return `linear-gradient(315deg, ${this.$store.state.baseSet.themeStyle.color_o}, ${this.$store.state.baseSet.themeStyle.color_t})`
			} else {
				return `linear-gradient(315deg, #FD463E, #ff3883)`
			}
		},
		isUserAudit() {
			const userStatus = this.$store.state.userStatus
			if (userStatus.enableStatus === 4) {
				return false
			} else if (userStatus.status === 3) {
				return false
			} else if (userStatus.status === 1) {
				return false
			} else if (userStatus.status === 0) {
				return false
			} else {
				return true
			}
		}
	},
	data() {
		return {
			navFixed: false,
			isLoding: false
			// amapPlugin: null
		}
	},

	onLoad() {
		//在 onLoad 中初始化一个高德小程序 SDK 的实例对象。
		this.amapPlugin = new amap.AMapWX({
			key: webUrl.GDMap_key
		});
	},
	methods: {
		callphone(phone) {
			uni.makePhoneCall({
				phoneNumber: phone
			})
		},
		goPage(url, type) {
			if(!url) return
			let urlT = ''
			if (!['/pages/index/index', '/pages/cart/cart', '/pages/classification/cate'].includes(url)) {
				const arr = url.split('/')
				arr[1] = 'pagesT'
				urlT = arr.join('/')
			} else {
				if (['/pages/index/Seckill'].includes(url)) {
					urlT = 'pagesT/seckill/Seckill'
				} else {
					urlT = url
				}
			}
			if (type === 'none') {
				return
			}
			if (type === 'switchTab') {
				uni.switchTab({
					url: url
				})
			} else if (type === 'redirectTo') {
				uni.redirectTo({
					url: urlT
				})
			} else if (type === 'reLaunch') {
				uni.reLaunch({
					url: urlT
				})
			} else {
				uni.navigateTo({
					url: urlT
				})
			}
		},

		userAudit() {
			// 判断用户status状态 0:待补全 1:待审核 2:已审核 3:审核失败 4:审核中
			const userStatus = this.$store.state.userStatus
			if (userStatus.enableStatus === 4) {
				uni.clearStorageSync();
				uni.showModal({
					title: '账号已被禁用，您可以',
					content: '联系电话：' + userStatus.enterpriseMobile,
					confirmText: '联系客服',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							uni.makePhoneCall({
								phoneNumber: userStatus.enterpriseMobile
							});
						} else {
							this.goPage('/pages/binding/bindInfo', 'reLaunch');
						}
					}
				});

				return;
			}
			// if (this.$store.state.baseSet.personnelReview === 5) {
			if (userStatus.status === 3) {
				// 说明资料不完善，跳转到完善资料的页面
				uni.showModal({
					title: '审核未通过审核，请重新修改资料',
					content: userStatus.auditFailReason || '',
					cancelText: '取消',
					confirmText: '修改资料',
					success: res => {
						if (res.confirm) {
							this.goPage('/pages/user/editUserInfo?token=' + this.$common.getToken());
						}
					}
				});
			} else if (userStatus.status === 1) {
				uni.showModal({
					title: '账号正在审核中，您可以',
					content: '联系电话：' + userStatus.enterpriseMobile,
					confirmText: '联系客服',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							uni.makePhoneCall({
								phoneNumber: userStatus.enterpriseMobile
							});
						}
					}
				});
			} else if (userStatus.status === 0) {
				uni.showModal({
					title: '请完善信息后再进行操作',
					content: '商家需要对客户资料进行审核，请您完善信息后再次进行操作',
					confirmText: '完善信息',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							this.goPage('/pages/user/editUserInfo');
						}
					}
				});
			}
			// }

		},
		backPage() {
			uni.navigateBack()
		},

		//授权
		getAuthorizeInfo() {
			this.getLocation();
		},
		// 再次获取授权 微信小程序
		// 当用户第一次拒绝后再次请求授权
		openConfirm() {
			uni.showModal({
				title: '请求授权当前位置',
				content: '商品信息将无法正常显示，请确认授权',
				success: res => {
					this.isLoding = true
					uni.openSetting({
						success: reso => {
							if (!reso.authSetting['scope.userLocation']) {
								this.openConfirm();
							}
						},
						complete: () => {
							this.isLoding = false
						}
					});
				}
			});
		},
		// 获取定位
		getLocation() {
			this.isLoding = true;
			let that = this;  
			// wx.authorize({
			// 	scope: 'scope.userFuzzyLocation',
			// 	success(res) {
			// 		if(res.errMsg == 'authorize:ok'){
			// 			wx.getFuzzyLocation({
			// 				type: 'wgs84',
			// 				success(res) {
			// 					const qqmapsdk = new QQMapWX({
			// 						key: webUrl.QQMap_key,
			// 					});
			// 					qqmapsdk.reverseGeocoder({
			// 						location: {
			// 							latitude: res.latitude,
			// 							longitude: res.longitude,
			// 						},
			// 						success: (addressRes) => {
			// 							console.log(addressRes);
									
			// 							const proCity = ['北京市', '天津市', '上海市', '重庆市']
			// 							const provinceName = addressRes.result.address_component.province
			// 							const cityName = proCity.includes(provinceName) ? provinceName : addressRes.result.address_component.city
			// 							const districtName = addressRes.result.address_component.district
			// 							const latitude = addressRes.result.location.latitude
			// 							const longitude = addressRes.result.location.longitude
										
			// 							const location = {
			// 								provinceName: provinceName,
			// 								cityName: cityName,
			// 								districtName: districtName,
			// 								latitude: latitude,
			// 								longitude: longitude
			// 							};
										
			// 							that.$store.commit('commit_locationObj', {
			// 								...location,
			// 								latitude: latitude,
			// 								longitude: longitude
			// 							});
			// 						},
			// 						fail: res => {
			// 							console.log(res)
			// 							wx.showToast({
			// 								title: res.message,
			// 								icon: "none",
			// 								duration: 2000,
			// 							})
			// 						}
			// 					});
			// 				}
			// 			})
			// 		}
			// 	},
			// 	fail(err) {
			// 		console.log(err)   
			// 	}                    
			// })
			
			
			/*
			this.amapPlugin.getRegeo({
				success: data => {
					const proCity = ['北京市', '天津市', '上海市', '重庆市']
					this.isLoding = false
					// console.log('获取定位:', data)
					const provinceName = data[0].regeocodeData.addressComponent.province
					console.log('获取定位:', provinceName)
					const cityName = proCity.includes(provinceName) ? provinceName : data[0].regeocodeData
						.addressComponent.city
					const districtName = data[0].regeocodeData.addressComponent.district
					// const address = data[0].regeocodeData.addressComponent.township
					const address = data[0].desc || data[0].regeocodeData.addressComponent.streetNumber
						.street || data[0].regeocodeData
						.addressComponent.township;
					const location = {
						provinceName: provinceName,
						cityName: cityName,
						districtName: districtName,
						address: address
					};
					// console.log(123, location)
					this.$store.commit('commit_locationObj', {
						...location,
						latitude: data[0].latitude,
						longitude: data[0].longitude
					});
					// this.getTemplate();
				},
				fail: err => {
					console.log('定位失败', err);
					// 拒绝授权
					// #ifdef MP
					this.openConfirm();
					// #endif
					// #ifdef APP-PLUS
					if (plus.os.name == "iOS") {
						let result = permision.judgeIosPermission('location')
						if (!result) {
							this.judgeIosPermission('location')
						}
					} else {
						this.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION')
					}
					// #endif
					// console.log('你拒绝了授权，无法获得周边信息');
				}

			});*/
		},
		// 再次获取授权 安卓
		async requestAndroidPermission(permisionID) {
			let result = await permision.requestAndroidPermission(permisionID)
			if (result == 1) {
				strStatus = "已获得授权"
			} else {
				// uni.showToast({
				// 	title: '未获取到您的位置信息,商品数据将无法正常显示,请前往【设置->应用->权限管理->位置】确认授权位置信息',
				// 	duration: 10000,
				// 	icon: 'none'
				// });
				// 设置->隐私->定位服务中开启定位服务
				uni.showModal({
					title: '定位服务已关闭',
					content: '您需要打开定位权限，才可以获取商品信息，请前往【设置->应用->权限管理->位置】中开启定位服务',
					success: (res) => {
						if (res.confirm) {
							permision.gotoAppPermissionSetting()
						}
					}
				});
			}
		},
		// 再次获取授权 IOS
		judgeIosPermission(permisionID) {
			var result = permision.judgeIosPermission(permisionID)
			// var strStatus = (result) ? "已" : "未"
			if (!result) {
				uni.showModal({
					title: '定位服务已关闭',
					content: '您需要打开定位权限，才可以获取商品信息，请前往【设置->隐私->定位服务】中开启定位服务',
					success: (res) => {
						if (res.confirm) {
							permision.gotoAppPermissionSetting()
						}
					}
				});
			}
		}

	}
}
